﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;
using XQ.Framework.io.text;
using XQ.WeChatOfficialAccount.BLL.Account;
using XQ.WeChatOfficialAccount.BLL.Sys;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Sys;
using XQ.WeChatOfficialAccount.WebAPI.Filter;
using XQ.WeChatOfficialAccount.WebAPI.Models;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.System
{
    /// <summary>
    /// 用户相关
    /// </summary>
    //[FormAuthenticationFilter]
    public class UserController : ApiController
    {
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/User/GetUserList")]
        public ResultModel GetUserList(int pageSize=10, int index=1, string userName="", string trueName = "", int deptID=1)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                Pagination page = new Pagination();
                UserBLL bll = new UserBLL();
                List<UserExModel> list = bll.GetUserList(pageSize, index, userName, trueName, page, deptID);
                resultModel.success = true;
                resultModel.code = "10000";
                resultModel.message = "执行成功";
                resultModel.data = list;
                resultModel.page = page;
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.code = "10001";
                resultModel.message = "执行失败";
                resultModel.data = ex.Message;
            }
            return resultModel;
        }

        /// <summary>
        /// 获取满足条件的用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="trueName"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/User/GetUsersNum")]
        public string GetUsersNum(string userName, string trueName)
        {
            UserBLL bll = new UserBLL();
            string allNum = bll.GetUsersNum(userName, trueName);
            return allNum;
        }

        /// <summary>
        /// 保存用户
        /// </summary>
        /// <returns></returns>
        [Route("Api/User/SaveUser")]
        public string SaveUser([FromBody]SaveUserExModel userExModel)
        {
            try
            {
                ////获取用户信息
                //IPrincipal principal = HttpContext.Current.User;
                //int userId = int.Parse(principal.Identity.Name);
                Sys_Users userModel = new Sys_Users();
                userModel.id = userExModel.id;
                userModel.IsAdmin = userExModel.IsAdmin;
                userModel.IsDel = userExModel.IsDel;
                userModel.IsDisabled = userExModel.IsDisabled;
                userModel.ModTime = userExModel.ModTime;
                userModel.ModUserID = userExModel.ModUserID;
                userModel.Password = userExModel.Password;
                userModel.Phone = userExModel.Phone;
                userModel.Remark = userExModel.Remark;
                userModel.DepartmentID = userExModel.DepartmentID;
                userModel.TrueName = userExModel.TrueName;
                userModel.UserName = userExModel.UserName;

                //保存用户
                if (userModel.id == 0)
                {
                    userModel.AddTime = DateTime.Now;
                }
                else
                {
                    userModel.ModTime = DateTime.Now;
                }
                UserBLL bll = new UserBLL();
                int userid = bll.SaveUser(userModel);
                //存在相同的用户名
                if (userid == 999999999)
                {
                    return "已存在相同的用户名！";
                }

                //保存用户与角色对应关系
                return SaveUserRole(userid, userExModel.RoleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/User/DeleteUser")]
        public string DeleteUser(int userid)
        {
            UserBLL bll = new UserBLL();
            if (bll.DeleteUser(userid))
            {
                return "success";
            }
            return "err";
        }

        /// <summary>
        /// 删除用户(多删除)
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/User/DeleteUsers")]
        public string DeleteUsers(List<int> userIds)
        {
            UserBLL bll = new UserBLL();
            return bll.DeleteUsers(userIds);
        }

        [Route("Api/User/GetUserByID")]
        public Sys_Users GetUserByID(int userid)
        {
            UserBLL bll = new UserBLL();
            return bll.GetUserID(userid);
        }

        /// <summary>
        /// 根据用户ID获取对应权限ID
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [Route("Api/User/GetRoleByUserID")]
        public int GetRoleByUserID(int userid)
        {
            UserBLL bll = new UserBLL();
            return bll.GetRoleByUserID(userid);
        }

        /// <summary>
        /// 保存用户对应的权限
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        [Route("Api/User/SaveUserRole")]
        public string SaveUserRole(int userid, int roleid)
        {
            UserBLL bll = new UserBLL();
            if (bll.SaveUserRole(userid, roleid))
            {
                return "ok";
            }
            return "err";
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("Api/User/UpdatePassword")]
        public bool UpdatePassword([FromBody]UpdatePasswordModel model)
        {
            Sys_Users user = LoginBll.Instance.GetUserByIDAndPassword(model.UserID, model.OldPassword);
            if (user != null)
            {
                UserBLL bll = new UserBLL();
                user.Password = model.NewPassword;
                return bll.SaveUser(user)==1;
            }
            return false;
        }

        /// <summary>
        /// 批量重置密码
        /// </summary>
        /// <param name="userIds">用户ID数组</param>
        /// <returns></returns>
        [Route("Api/User/ReSetPassword")]
        public bool ReSetPassword(List<int> userIds)
        {
            UserBLL bll = new UserBLL();
            List<Sys_Users> users = bll.GetUsersByIds(userIds);
            if (users != null && users.Count>0)
            {
                foreach (Sys_Users user in users)
                {
                    user.Password = "123456";
                    bll.SaveUser(user);
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 设置启用
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        [Route("Api/User/SetEnable")]
        public bool SetEnable(List<int> userIds)
        {
            UserBLL bll = new UserBLL();
            List<Sys_Users> users = bll.GetUsersByIds(userIds);
            if (users != null && users.Count > 0)
            {
                foreach (Sys_Users user in users)
                {
                    user.IsDisabled = false;
                    bll.SaveUser(user);
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 设置禁用
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        [Route("Api/User/SetDisabled")]
        public bool SetDisabled(List<int> userIds)
        {
            UserBLL bll = new UserBLL();
            List<Sys_Users> users = bll.GetUsersByIds(userIds);
            if (users != null && users.Count > 0)
            {
                foreach (Sys_Users user in users)
                {
                    user.IsDisabled = true;
                    bll.SaveUser(user);
                }
                return true;
            }
            return false;
        }

        

    }
}